/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BackingBeans;

import Facebook.FacebookClientLocal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import smartgrid.EltricStatsManagerLocal;
import smartgrid.RankingElem;

/**
 *
 * @author andre
 */
@ManagedBean
@RequestScoped
public class RankingBackingBean {

    private Map<String, String> UIds;
    private TreeSet<RankingElem> Rank;

    /** Creates a new instance of RankingBackingBean */
    public RankingBackingBean() {
        try {
            InitialContext initCtx = new InitialContext();
            EltricStatsManagerLocal am = (EltricStatsManagerLocal) initCtx.lookup("java:global/SmartGridEA/EltricStatsManager");

            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            FacebookClientLocal fbl = null;
            fbl = (FacebookClientLocal) request.getSession(true).getAttribute("facebook");

            this.UIds = fbl.getUserFriendsUIds();

            ArrayList<String> ids = new ArrayList<String>();
            for (String s : this.UIds.keySet()) {
                ids.add(s);
            }

            this.Rank = am.getRanking(ids);

        } catch (NamingException ex) {
            Logger.getLogger(RankingBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getCategories() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Iterator<RankingElem> it = this.Rank.iterator();
        while (it.hasNext()) {
            sb.append("'" + this.UIds.get(it.next().getuID()) + "'");
            if (it.hasNext()) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public String getValues() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Iterator<RankingElem> it = this.Rank.iterator();
        while (it.hasNext()) {
            sb.append(it.next().getValor());
            if (it.hasNext()) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public String getImages() {
        StringBuilder sb = new StringBuilder();
        for (RankingElem r : this.Rank) {
            sb.append("<li class=\"fotos\"><div class=\"friend\"><img src=\"http://graph.facebook.com/");
            sb.append(r.getuID());
            sb.append("/picture\" /><span>");
            sb.append(this.UIds.get(r.getuID()));
            sb.append("</span></div></li>");
        }
        return sb.toString();
    }
}
